package com.wssnail.oss.template;

import com.wssnail.oss.myenum.FileTypeEnum;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author: 熟透的蜗牛
 * @CreateTime: 2022-07-11 14:38
 * @Description: 上传图片的抽象类
 * @Version: 1.0
 */
public abstract class OssTemplate {

    /*
     * @description:校验文件格式
     * @Author: 熟透的蜗牛
     * @date: 2022/7/11 14:49
     * @param is
     * @return:
     * @return: com.bls.oss.myenum.FileTypeEnum
     **/
    public FileTypeEnum checkFileType(InputStream is) throws IOException {
        byte[] src = new byte[28];
        is.read(src, 0, 28);
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        FileTypeEnum[] fileTypes = FileTypeEnum.values();
        for (FileTypeEnum fileType : fileTypes) {
            if (stringBuilder.toString().startsWith(fileType.getValue())) {
                return fileType;
            }
        }
        return null;
    }

    public abstract Map upload(MultipartFile file);

    public abstract Map deleteFile(String fileName);

    public abstract String downLoadFile(HttpServletResponse response, String fileName);

    /*
     * @description:分片上传
     * @Author: 熟透的蜗牛
     * @date: 2022/6/23 13:48
     * @param: file
     * @return: java.util.Map
     **/
     public abstract Map chunkFile(MultipartFile file);

}
